﻿
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 
 * C O P Y R I G H T    N O T I C E
 * 
 * Copyright (c) 2000-2010 Acidaes Solutions Pvt. Ltd. All Rights Reserved.
 * 
 * This code is a part of the source code used by Acidaes Solution Private Limited for its product "BusinessNext".
 * You may not, directly or indirectly, use, disclose, distribute, print or copy this code or any part of it.
 * 
 * This code contains confidential information and/or may contain information protected by intellectual property 
 * rights or other rights. This code shall/may not be construed to constitute any commitment from 
 * Acidaes Solutions Private Limited or its subsidiaries or affiliates except when expressly agreed to in a 
 * separate written agreement.
 * 
 * AUTHORS:                           DATE:
 * 
 * REVISION: $Revision: 5420 $
 * LAST MODIFIED BY: &Author$ on $Date: 2007-06-16 12:01:05 +0530 (Sat, 16 Jun 2007) $  
 * 
 * R E V I S I O N  H I S T O R Y:
 * 
 * DATE      CHANGED BY               COMMENT     
 * 
 *  
 * C O D E  R E V I E W  H I S T O R Y: 
 * 
 * DATE      REVIEWED BY              COMMENT     
 *  
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Provider;
using System.Web.Configuration;
using System.Configuration;
using System.Web;



namespace Acidaes.Caching
{
    public sealed class CacheClient : ICacheProvider
    {
        private static CacheProvider _provider = null;
        private static CacheProviderCollection _providers = null;
        private static object _lock = new object();

        public CacheClient()
        {
            LoadProviders();
        }

        public CacheProvider Provider
        {
            get { return _provider; }
        }

        public CacheProviderCollection Providers
        {
            get { return _providers; }
        }

        public  Object Get(string key)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.Get(key);

        }

        public object Get(string key,ref CacheMemberVersion version)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.Get(key,ref version);

        }


        public Object Add(string key, object value)
        {
            return _provider.Add(key,value);
        }

        public object Add(string region, string key, Object value, TimeSpan timeOut)
        {
            return _provider.Add(region, key, value, timeOut);
        }

        /// <summary>
        /// Adds a new object to the cache or replaces one if it already is located in the cache.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public  Object Put(string key, object value)
        {
            // Make sure a provider is loaded
            LoadProviders();

            // Delegate to the provider
            return _provider.Put(key,value);
        }

        public void Remove(string key)
        {
            LoadProviders();

            _provider.Remove(key);
        }

        public void RemoveAll()
        {
            if (_provider != null)
                _provider.RemoveAll();
        }

        private static void LoadProviders()
        {
            // Avoid claiming lock if providers are already loaded
            if (_provider == null)
            {
                lock (_lock)
                {
                    // Do this again to make sure _provider is still null
                    if (_provider == null)
                    {
                        // Get a reference to the <cacheProvider> section
                        CacheProviderSection section = (CacheProviderSection)
                            WebConfigurationManager.GetSection("acidaes.caching/cacheProvider");

                      // _provider= CacheProviderFactory.Load();
                        // Load registered providers and point _provider
                        // to the default provider
                        _providers = new CacheProviderCollection();
                        ProvidersHelper.InstantiateProviders
                            (section.Providers, _providers,
                            typeof(CacheProvider));
                        _provider = _providers[section.DefaultProvider];

                        if (_provider == null)
                            throw new ProviderException
                                ("Unable to load default Cache Provider");
                    }
                }
            }
        }



       
    }
}
